<?php
namespace App\Http\Controllers\Business\App\Today;

use App\Models\Banner;
use App\Models\AppStart;
use App\Models\Shop;

class Today
{
    public static function index($cityid)
    {
        //更新banner状态
        $GLOBALS['cityid'] = $cityid;
        $updateBanner = Banner::where('status', 0)->where('starttime', date('Y-m-d'))->update(['status'=>1]);
        $banner = Banner::where('status', 1)
            ->where(function($query) {
                $query->orwhere('isglobal','1')
                    ->orWhere('cityid', $GLOBALS['cityid']);
            })
            ->orderBy('sort','asc')->select('name','url as picurl','type', 'content','sort', 'starttime', 'lasttime', 'islogin')->get();
        $category = array(
            array(
                'id' => 1,
                'name' => '美食',
                'isfirstclass' => 1,
                'firstclassid' => 1,
                'logo' => 'images/food.png'
            ),
            array(
                'id' => 7,
                'name'=>'甜点饮品',
                'isfirstclass' => 0,
                'firstclassid' => 1,
                'logo'=>'images/dessert.png'
            ),
            array(
                'id' => 2,
                'name' => '休闲娱乐',
                'isfirstclass' => 1,
                'firstclassid' => 2,
                'logo' => 'images/recreation.png'
            ),
            array(
                'id' => 3,
                'name' => '生活服务',
                'isfirstclass' => 1,
                'firstclassid' => 3,
                'logo'=> 'images/service.png'
            ),
            array(
                'id' => 9,
                'name' => 'KTV',
                'isfirstclass' => 0,
                'firstclassid' => 2,
                'logo'=>'images/ktv.png'
            ),
            array(
                'id' => 4,
                'name'=>'酒店',
                'isfirstclass' => 1,
                'firstclassid' => 4,
                'logo'=>'images/hotel.png'
            ),
            array(
                'id' => 20,
                'name'=>'服饰购物',
                'isfirstclass' => 0,
                'firstclassid' => 3,
                'logo'=> 'images/shopping.png'
            ),
            array(
                'id' => 0,
                'name'=>'全部',
                'isfirstclass' => 1,
                'firstclassid' => 0,
                'logo'=>'images/all.png'
            ),
        );
        $occur = [];
        if(Shop::where('cityid', $cityid)->where('firstclassid',4)->where('nightevent',1)->where('forbidnightevent', 0)->where('status', 1)->first()) {
            array_push($occur, array('id'=>4,'name'=>'酒店', 'isfirstclass'=>1));
        }
        if(Shop::where('cityid', $cityid)->where('secondclassid',9)->where('nightevent',1)->where('forbidnightevent', 0)->where('status', 1)->first()) {
            if(empty($occur)) {
                array_push($occur, array('id'=>9,'name'=>'KTV','isfirstclass'=>0));
            } else {
                array_push($occur, array('id'=>9,'name'=>'KTV', 'isfirstclass'=>0));
            }
        }
        $data['banner'] = $banner;
        $data['category'] = $category;
        $data['occur'] = $occur;
        return ['status'=>'success','data'=>$data];
    }
    //首页banner
    public static function getIndexBanner($cityId) {
        $GLOBALS['cityid'] = $cityId;
        $banner = Banner::where('status', 1)->where(function($query) {
            $query->orwhere('isglobal','1')
                ->orWhere('cityid', $GLOBALS['cityid']);
            })->select('name','url','type', 'content')->get();
        return ['status'=>'success', 'data'=>$banner];
    }
    //首页菜单分类
    public static function getIndexClass() {
        $category = array(
            array(
                'id' => 1,
                'name' => '美食',
                'isfirstclass' => 1,
                'logo' => 'images/food.png'
            ),
            array(
                'id' => 2,
                'name' => '休闲娱乐',
                'isfirstclass' => 1,
                'logo'=>'images/recreation.png'
            ),
            array(
                'id' => 3,
                'name' => '生活服务',
                'isfirstclass' => 1,
                'logo'=>'images/service.png'
            ),
            array(
                'id' => 9,
                'name' => 'KTV',
                'isfirstclass' => 0,
                'logo'=>'images/ktv.png'
            ),
            array(
                'id' => 4,
                'name'=>'酒店',
                'isfirstclass' => 1,
                'logo'=>'images/hotel.png'
            ),
            array(
                'id' => 20,
                'name'=>'服饰购物',
                'isfirstclass' => 0,
                'logo'=>'images/shopping.png'
            ),
            array(
                'id' => 7,
                'name'=>'甜点饮品',
                'isfirstclass' => 0,
                'logo'=>'images/dessert.png'
            ),
            array(
                'id' => 0,
                'name'=>'全部',
                'isfirstclass' => 1,
                'logo'=>'images/all.png'
            ),
        );
        return ['status'=>'success', 'data'=>$category];
    }
    //首页21点活动
    public static function getIndexOccur($cityid) {
        $occur = [];
        if((Shop::where('cityid', $cityid)->where('firstclassid',4)->where('status', 1)->where('nightevent',1)->where('forbidnightevent', 0)->get()->count()) > 0) {
            array_push($occur, array('id'=>4,'name'=>'酒店', 'isfirstclass'=>1));
        }
        if((Shop::where('cityid', $cityid)->where('secondclassid',9)->where('status', 1)->where('nightevent',1)->where('forbidnightevent', 0)->get()->count()) > 0) {
            array_push($occur, array('id'=>9,'name'=>'KTV','isfirstclass'=>0));
        }
        return ['status'=>'success', 'data'=>$occur];
    }
    //app启动页
    public static function getAppStartList($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误！'];
        }
        $GLOBALS['cityid'] = $cityId;
        $data = AppStart::where('status','!=', 2)
                ->where(function($query) {
                    $query->orwhere('isglobal', '1')
                        ->orwhere('cityid', $GLOBALS['cityid']);
                })
            ->select('id', 'name', 'picurl', 'type', 'content', 'starttime', 'lasttime', 'status')->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //更新app状态
    public static function updateAppStartStatus($cityId,$ids=[]) {
        if(empty($ids) || !$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $GLOBALS['cityid'] = $cityId;
        //过期处理
        $handlePastDue = AppStart::where('status', '!=', 2)->where('lasttime', '<', date('Y-m-d'))
            ->where(function($query) {
                $query->orwhere('isglobal', '1')
                    ->orwhere('cityid', $GLOBALS['cityid']);
            })->update(['status'=>2]);
        //待发布变成已发布
        $handleWait = AppStart::whereIn('id', $ids)->where('status', 0)
            ->where(function($query) {
                $query->orwhere('isglobal', '1')
                    ->orwhere('cityid', $GLOBALS['cityid']);
            })->update(['status'=>1]);
        return ['status'=>'success', 'message'=>'更新成功'];
    }
}